<template>
  <div class="myTree">
    <a-input-search
      style="margin-bottom: 8px"
      placeholder="Search"
      @change="onChange"
    />
    <a-tree
      show-icon
      :showLine="true"
      :expanded-keys="expandedKeys"
      :auto-expand-parent="autoExpandParent"
      :tree-data="gData"
      @expand="onExpand"
      @select="onSelect"
    >
      <!-- 自定义图标slot，当数据中存在slots: { icon: "meh" }属性时，会使用对应图标 -->
      <a-icon slot="smile" type="pie-chart" />
      <a-icon slot="meh" type="smile-o" />
      <template slot="title" slot-scope="{ title }">
        <span v-if="title.indexOf(searchValue) > -1">
          {{ title.substr(0, title.indexOf(searchValue)) }}
          <span style="color: #f50">{{ searchValue }}</span>
          {{ title.substr(title.indexOf(searchValue) + searchValue.length) }}
        </span>
        <span v-else>{{ title }}</span>
      </template>
    </a-tree>
  </div>
</template>

<script>
const x = 3;
const y = 2;
const z = 1;
const gData = [];

const generateData = (_level, _preKey, _tns) => {
  const preKey = _preKey || "0";
  const tns = _tns || gData;

  const children = [];
  for (let i = 0; i < x; i++) {
    const key = `${preKey}-${i}`;
    tns.push({
      title: key,
      key,
      scopedSlots: { title: "title" },
    });
    if (i < y) {
      children.push(key);
    }
  }
  if (_level < 0) {
    return tns;
  }
  const level = _level - 1;
  children.forEach((key, index) => {
    tns[index].children = [];
    return generateData(level, key, tns[index].children);
  });
};
generateData(z);

const dataList = [];
const generateList = (data) => {
  for (let i = 0; i < data.length; i++) {
    const node = data[i];
    const key = node.key;
    dataList.push({ key, title: key });
    if (node.children) {
      generateList(node.children);
    }
  }
};
generateList(gData);

gData.push({
  title: "额外的节点",
  key: "other-root",
  slots: { icon: "meh" },
});
gData.push({
  title: "额外的节点2",
  key: "other-root-2",
  slots: { icon: "smile" },
  children: [
    {
      title: "叶子",
      key: "leaf",
      slots: { icon: "meh" },
    },
  ],
});
const getParentKey = (key, tree) => {
  let parentKey;
  for (let i = 0; i < tree.length; i++) {
    const node = tree[i];
    if (node.children) {
      if (node.children.some((item) => item.key === key)) {
        parentKey = node.key;
      } else if (getParentKey(key, node.children)) {
        parentKey = getParentKey(key, node.children);
      }
    }
  }
  return parentKey;
};
export default {
  data() {
    return {
      expandedKeys: [],
      searchValue: "",
      autoExpandParent: true,
      gData,
    };
  },
  methods: {
    onExpand(expandedKeys) {
      this.expandedKeys = expandedKeys;
      this.autoExpandParent = false;
    },
    onSelect(selectedKeys) {
      console.log(selectedKeys);
      if (selectedKeys && selectedKeys.length > 0) {
        this.$emit("select", selectedKeys[0]);
      }
    },
    onChange(e) {
      const value = e.target.value;
      const expandedKeys = dataList
        .map((item) => {
          if (item.title.indexOf(value) > -1) {
            return getParentKey(item.key, gData);
          }
          return null;
        })
        .filter((item, i, self) => item && self.indexOf(item) === i);
      Object.assign(this, {
        expandedKeys,
        searchValue: value,
        autoExpandParent: true,
      });
    },
  },
};
</script>
<style scoped>
.myTree ::v-deep(.anticon-file) {
  /* 隐藏文件图标 */
  /* visibility: hidden; */
  /* display: none; */
  /* border-bottom: 1px solid #d9d9d9;
  position: absolute;
  height: 150%;
  top:-100%;
  width: 50%;
  left: 50%;
  border-left: 1px solid #d9d9d9; */
}
.myTree ::v-deep(.anticon-file svg) {
  /* display: none; */
}
</style>
